package m4;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");  // wht6.cn
        // f.setPort(5672); //默认端口可以不设置
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        // 定义direct类型的交换机： direct_logs
        c.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);

        while (true) {
            System.out.print("输入消息： ");
            String msg = new Scanner(System.in).nextLine();
            System.out.print("输入路由键： ");
            String key = new Scanner(System.in).nextLine();
            // 发送消息，并携带路由键
            c.basicPublish("direct_logs",key,null,msg.getBytes());
        }
    }
}
